mui.init();
showLoading();
var max = document.documentElement.clientWidth;
var Wcitylist; //城市列表页
var adUrl = getUrl("75getAdvert"); //广告url
var noCircleurl = getUrl("57noCircle"); //未关注圈子url
var ocityNoCircle = document.getElementById("bg");
var mycircle = document.getElementById("mycircle");
/*		 
 * 初始化图片 使得图片大小统一
 * listimg为页面中的图片对象
 */
function initImg(listimg) {
	listimg.parentNode.style.height = (max - 31) / 3.0 + "px";
}
//vue实例化对象
var home = new Vue({
		el: "#home",
		methods: {
			loadMore: loadMore,
			opencity: opencity,
			openGuanzhu: openGuanzhu,
			openCircle: openCircle,
			openProduct: detail,
			openMessage: openMessage
		},
		data: {
			Circles: [], //未关注圈子
			myCircles: []
		},
		ready: function() {
			//请求广告
			this.$http({
				url: adUrl,
				method: 'post',
				params: {
					advertLevel: "system"
				}
			}).then(function(repson) {
				console.log("广告：" + JSON.stringify(repson));
				var data = repson.data;
				if (data.lunList.tops[0]) {
					var toplunList = data.lunList.tops[0].images.split(',');
					this.$set('sliderPic', toplunList);
				}
				if (data.textList.tops) {
					this.$set('toptextlist', data.textList.tops)
				}
				if (data.picList.mids[0]) {
					var picAd = data.picList.mids[0].images;
					this.$set('picAd', picAd);
				}
				setTimeout(function() {
					initSwiper();
				}, 100)
			}, function(response) {});
			//取得登陆后关注的圈子
			if (localStorage.account) {
				 //hasUread(readData(localStorage.noticeToken));//未读消息提示
				 loginRong();//融云登录
				var circleurl = getUrl("circleList"); //关注圈子
				var headerJson = getHeader();
				var cid = readData(localStorage.account);
				this.$http({
					url: circleurl,
					method: 'post',
					headers: headerJson,
					data: 'id=' + cid
				}).then(function(repson) {
					console.log("我的圈子数据：" + JSON.stringify(repson));
					var data = repson.data;
					if (data.code == "0") {
						mycircle.style.display = "block";
						var circle = data.obj.list;
						for (var i = 0; i < circle.length; i++) {
							for (var pic in circle[i].productList) {
								if (!circle[i].productList[pic]["picture"]) {
									circle[i].productList[pic]["picture"] = "../../image/small.png";
									
								}
							}
						}
						this.$set('myCircles', circle)
							//$scope.myCircles = circle;
						guanzhucid = data.obj.list;
						if (data.obj.list.length <= 0) {
							$scope.imghide = true;
						}
					} else {
						console.log("网络链接异常，请稍后重试" + data.code)
					}
				}, function(response) {});
			}
		}
	})
	//下拉加载未关注圈子
var ipagenum = 0;
var ipagesizenum = 3;
var Olocation = document.getElementById("location")
	//初始化推荐圈子加载下拉
function initLoadMore() {

	ipagenum = 0;
	ipagesizenum = 3;
	home.Circles = [];
	//默认加载未关注圈子
	home.loadMore();
}
//未关注圈子请求
function loadMore() {
	var params1;
	if (localStorage.currentCity && localStorage.currentCity != "") {
		Olocation.innerText = readData(localStorage.currentCity);
		params1 = {
			"indexpage": ipagenum,
			"pageSize": ipagesizenum,
			"city": readData(localStorage.currentCity)
		}
	} else {
		params1 = {
			"indexpage": ipagenum,
			"pageSize": ipagesizenum
		}
	}
	home.$http({
		url: noCircleurl,
		method: 'post',
		params: params1
	}).then(function(repson) {
		console.log("推荐圈子数据：" + JSON.stringify(repson));
		ipagenum++;
		var data = repson.data;
		if (data.obj) {
			var circle = data.obj.list;
			for (var i = 0; i < circle.length; i++) {
				for (var pic in circle[i].productList) {
					if (!circle[i].productList[pic]["picture"]) {
						circle[i].productList[pic]["picture"] = "../../image/small.png"
					}
				}
				home.Circles.push(circle[i]);
			}
			if (data.obj.totalsize <= 0) {
				ocityNoCircle.style.display = "block";
				mui(".mui-pull-bottom-tips")[0].style.display = "none";
			} else {
				ocityNoCircle.style.display = "none";
				mui(".mui-pull-bottom-tips")[0].style.display = "block";
			}

		} else {
			nocircle.style.display = "block";
			mui(".mui-pull-bottom-tips").style.display = "none";
		}
		removeLoading()
	}, function(response) {
		removeLoading()
	});
}
initLoadMore();
//轮播图初始化
function initSwiper() {
	var mySwiper = new Swiper('.swiper-container', {
		direction: 'horizontal',
		loop: true,
		autoplay: 5000,
		speed: 1000,
		pagination: '.swiper-pagination'
	});
	new Slide("outwrapper", "inslide", 3000).init();
}
//下拉加载
var bRefresh = true;
mui(".mui-scroll").pullToRefresh({
	up: {
		callback: function() {
			var self = this;
			if (bRefresh) {
				bRefresh = false;
				setTimeout(function() {
					//var ul = self.element.querySelector('.mui-table-view');
					//ul.appendChild(createFragment(ul, index, 5));
					home.loadMore();
					self.endPullUpToRefresh();
					bRefresh = true;
				}, 1000);
			}
		}
	}
});
//监听事件 城市列表页面出发changecity事件
window.addEventListener("changeCity", function() {
		initLoadMore();
	})
	//预加载城市列表页
document.addEventListener("plusready", function() {
	Wcitylist = mui.preload({
		url: "cityList.html",
		id: "cityList.html"
	});
	Wcitylist.hide();
}, false);
//根据滚动事件控制头部变化
window.onscroll = myFunction;

function myFunction() {
	//console.log(document.body.scrollTop);
	var topscroll = document.body.scrollTop;
	var header = mui("#header")[0];
	var backtop = document.getElementById("to_top");
	var msg=document.getElementById('info');
	if (topscroll > 50) {
		msg.style.borderColor= '#fff';
		header.style.background = "#da4453";
		header.style.color = "#fff";
		if (topscroll > 400) {
			backtop.style.display = "block";
		}
	} else {
		msg.style.borderColor= 'red';
		header.style.background = "rgba(255, 255, 255, .5)";
		header.style.color = "#000";
		backtop.style.display = "none";
	}
}
//打开城市列表页
function opencity() {
	Wcitylist.show("slide-in-bottom");
}
function openMessage(){
	mui.openWindow({		
		url: 'my/message.html',
		id: 'message.html',
		waiting: {
			autoShow: false
		}
	})
}
function openGuanzhu(uid, groupId) {
	localStorage.guideId = writeData(uid);
	OpenNewWin({
		url: "shopping/guanzhu.html",
		id: 'guanzhu.html'
	})
}
//打开圈子进入
function openCircle(gid, cname, groupId, cId) {
	localStorage.guideId = writeData(gid);
	localStorage.circleName = writeData(cname);
	localStorage.groupId = writeData(groupId);
	localStorage.circleId = writeData(cId);
	OpenNewWin({
		url: "shopping/circleMain.html",
		id: "circleMain.html"
	})
}
//打开商品详情
function detail(id, gid, cname, groupId) {
	//获取商品ID，打开商品详情页
	localStorage.goodsId = writeData(id);
	localStorage.guideId = writeData(gid);
	localStorage.circleName = writeData(cname);
	localStorage.groupId = writeData(groupId);
	OpenNewWin({
		url: "shopping/productDetail.html",
		id: "productDetail.html"
	})
}
